var base_url="http://"+location.host+"/";
var catalog_id=$(".catalog").attr("data_catalog_id");
var book_id=$(".catalog").attr("data_book_id");
var page=$(".catalog").attr("data_page");
var req_url=base_url+"api/catalog/"+book_id;
var cur_index=-1;
var prev_id=-1;
var next_id=-1;
function get_catalog_id(){
    var num=50;
    var order="asc";
    $.ajax({
        url:req_url,
        type:"POST",
        data:{
            page:page,
            num:num,
            order:order
        },
        success:function(res){
            //console.log(res);
            res.data.forEach(function(v,i){
                if(v.id==catalog_id){
                    cur_index=i
                    if(v.vip=="Y"){
                        $(".catalog").html('<div class="weui-flex__item buy" style="margin:5px"><a href="javascript:;" class="weui-btn weui-btn_primary buy"><h5>订阅本章</h5></a></div>');
                        $(".buy").click(function(e){
                            $.alert("不能订阅~^~");
                        });
                    }
                }

            });
            if(cur_index!=-1){
                if(cur_index==1||cur_index==res.data.length){
                    return;
                }
                prev_id=res.data[cur_index-1].id;
                next_id=res.data[cur_index+1].id;
                return true
            }
            return false;
        }
    });
}

get_catalog_id();

$(".prev_catalog").click(function(e){
    console.log(prev_id);
    if(prev_id!=-1){
       location.href=base_url+"catalog_detail/"+book_id+"/"+prev_id+"?page="+page;
    }else{
        $.alert("没有上一章了");
    }

});

$(".next_catalog").click(function(e){
    console.log(next_id);
    if(next_id!=-1){
        location.href=base_url+"catalog_detail/"+book_id+"/"+next_id+"?page="+page;
    }else{
        $.alert("没有下一章了");
    }

});

